package org.lex.ftplist.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import org.lex.ftplist.core.FtpUrl;
import org.lex.ftplist.ui.common.FLDialog;
import org.lex.ftplist.ui.common.OpenFtpPane;

public class OpenFtpDialog extends FLDialog {
	private OpenFtpPane pane = null;
	private boolean canceled = false;

	public OpenFtpDialog() {
		this.setTitle("Open FTP");
		this.setModal(true);
		this.setSize(400, 300);
		this.moveToParentCenter();
		this.initComponents();
	}

	public FtpUrl getUrl() {
		if (canceled) {
			return null;
		} else {
			return pane.getUrl();
		}
	}

	@Override
	protected void windowClosing(WindowEvent e) {
		this.canceled = true;
	}

	private void dispose(boolean canceled) {
		this.canceled = canceled;
		dispose();
	}

	private void initComponents() {
		pane = new OpenFtpPane();
		pane.getOkButton().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose(false);
			}
		});

		pane.getCancelButton().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose(true);
			}
		});
		this.setContentPane(pane);
		this.pack();
	}
}
